package com.finchina.myview.shader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import com.finchina.myview.R;

/**
 * 作者：黄赛赛 on 2015/9/18 14:06
 *
 * @version V1.0
 * @Description: ${todo}()
 */
public class BitmapShaderView extends View {
    Paint mPaint;
    /**
     * 渲染图像，使用图像为绘制图形着色
     */
    private BitmapShader mBitmapShader;
    public BitmapShaderView(Context context) {
        this(context, null);
    }

    public BitmapShaderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BitmapShaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 实例化画笔
        mPaint= new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        //Shader.TileMode.REPEAT MIRROR  CLAMP
        mBitmapShader = new BitmapShader(bmp, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
        //设置缩放
//        Matrix mx =  new Matrix();
//        mx.setScale(0.5f,0.5f);
//        mBitmapShader.setLocalMatrix(mx);
        mPaint.setShader(mBitmapShader);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);

//        int width = getWidth();
//        int height = getHeight();
//        int radius = (int) (Math.min(width,height)/2 *0.7f);
//       canvas.drawCircle(getWidth()/2, getHeight()/2, radius, mPaint);
        canvas.drawColor(Color.CYAN);
        canvas.drawRect(0,0,getWidth(),getHeight(),mPaint);
    }
}
